package project.taobao.microsoft.transaction.provider.one.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import project.taobao.commons.activemq.template.ActiveMqTemplate;
import project.taobao.microsoft.transaction.api.pojo.Order;
import project.taobao.microsoft.transaction.api.pojo.OrderItem;
import project.taobao.microsoft.transaction.provider.one.dao.OrderMapper;
import project.taobao.microsoft.transaction.provider.one.service.CalculateService;
import project.taobao.microsoft.transaction.provider.one.service.OrderItemService;
import project.taobao.microsoft.transaction.provider.one.service.OrderService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/transaction")
@PropertySource({"classpath:config/config.properties"})
public class TransactionController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private CalculateService calculateService;

    @Value("${shoppingCartAllInfomationQueue}")
    private String shoppingCartAllInfomationQueue;

    @Value("${commodityInformationQueue}")
    private String commodityInformationQueue;

    @Value("${isMemberQueue}")
    private String isMemberQueue;

    @Value("${addressQueue}")
    private String addressQueue;
    // methods

    /**
     * 计算购物车内选择商品的金额
     * @param shoppingList json字符串
     * @return
     */
    @ResponseBody
    @PostMapping("/shoppingCart/calculateMoney")
    public BigDecimal calculateMoney(@RequestBody List<ShoppingCart> shoppingList, HttpServletRequest request){
        BigDecimal allMoney = 0D;
        User user = (User)request.getAttribute("user");
        ActiveMqTemplate.sendMessage(isMemberQueue, user.getUserId());
        Boolean isMember = ((Integer) ActiveMqTemplate.consumerMessage(isMemberQueue)) > 0 ? true : false;
        for(ShoppingCart item : commodityList){
            allMoney += item.getPrice() * item.getNum();
        }
        allMoney = calculateService.calculateMoney(allMoney,isMember);
        return allMoney;
    }

    /**
     * 生成订单并扣除金额
     * @param shoppingList json字符串
     * @return
     */
    @ResponseBody
    @PostMapping("/shoppingCart/calculateMoney")
    @Transactional(propagation = )
    public boolean calculateMoney(@RequestBody List<ShoppingCart> shoppingList, BigDecimal money, HttpServletRequest request,
                                 Long addressId){
        List<Double> list = new ArrayList<>();
        List<OrderItem> orderItemList = new ArrayList<>();
        User user = (User)request.getAttribute("user");
        for(ShoppingCart s : shoppingList){
            OrderItem orderItem = new OrderItem();
            orderItem.setCommodityId(s.getCommodityId());
            orderItem.setCommodityTypeId(s.getCommodityTypeId);
            orderItem.setCommodityNum(s.getCommodityNum());
            // 购物车的price是折前还是折后
            orderItem.setCommoditySum(s.getPrice() / s.getCommodityNum());
            orderItem.setTotal(s.getPrice());
            orderItemList.add(orderItem);
        }
        Order order = new Order();
        order.setUsersId(user.getId());
        ActiveMqTemplate.sendMessage(addressQueue, addressId);
        Address address = ActiveMqTemplate.consumerMessage(addressQueue);
        order.setUserName(address.getContact());
        // adress字段没有
        order.setUserSex("男");
        order.setUserPhone(address.getPhone());
        order.setAddressId(addressId);
        order.setSum(money);
        // 存疑
        order.setPayment(Byte.parseByte("1"));
        // 格式是啥？
        order.setCreatetime(new Date());
        // 存疑
        order.setSigned(Byte.parseByte("1"));
        order = orderService.insertWithReturn(order);
        for(OrderItem s : orderItemList){
           s.setOrderId(order.getId());
           orderItemService.insert(s);
        }
        return true;

    }



    // getters, setters

    // constructors
}
